package com.etymgiko.lyricshere.manager.impl;

import com.etymgiko.lyricshere.manager.SongManager;
import com.etymgiko.lyricshere.manager.EMF;
import com.etymgiko.lyricshere.model.Song;
import com.etymgiko.lyricshere.model.Artist;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 *
 * @author Ivan Holub
 */
public class SongManagerImpl implements SongManager {
    public Song getSong(Long id) {
        EntityManager em = EMF.get().createEntityManager();
        try {
            Song s = em.find(Song.class, id);
            s.getLyrics();
            return s;
        } finally {
            em.close();
        }
    }

    public void saveSong(Song song) {
        EntityManager em = EMF.get().createEntityManager();
        try {
            em.persist(song);
        } finally {
            em.close();
        }
    }

    public List<Song> getSongs(Long artistId) {
        EntityManager em = EMF.get().createEntityManager();
        try {
            Query query = em.createQuery("select from " + Song.class.getName() + " s where s.artistId = ?1");
            query.setParameter(1, artistId);
            List<Song> list = query.getResultList();
            list.size();
            return list;
        } finally {
            em.close();
        }
    }

    public void deleteSong(Long id) {
        EntityManager em = EMF.get().createEntityManager();
        try {
            Song s = em.find(Song.class, id);
            em.remove(s);
        } finally {
            em.close();
        }
    }

    public void updateSong(Song song) {
        EntityManager em = EMF.get().createEntityManager();
        try {
            Song s = em.find(Song.class, song.getId());
            s.setTitle(song.getTitle());
            s.setCode(song.getCode());
            s.setLyrics(song.getLyrics());
            em.persist(s);
        } finally {
            em.close();
        }
    }

    public Song findSongByCode(Long artistId, String code) {
        EntityManager em = EMF.get().createEntityManager();
        try {
            Query query = em.createQuery("select from " + Song.class.getName() + " s where s.artistId = ?1 and s.code = ?2");
            query.setParameter(1, artistId);
            query.setParameter(2, code);
            Song song = null;
            List<Song> list = query.getResultList();
            if (list.size() > 0) {
                song = list.get(0);
            }
            return song;
        } finally {
            em.close();
        }
    }

    public List<Song> getSongs() {
        EntityManager em = EMF.get().createEntityManager();
        try {
            Query query = em.createQuery("select from " + Song.class.getName());
            List<Song> list = query.getResultList();
            list.size();
            return list;
        } finally {
            em.close();
        }
    }

    public Song findSongByTitle(Long artistId, String title) {
        EntityManager em = EMF.get().createEntityManager();
        try {
            Query query = em.createQuery("select from " + Song.class.getName() + " s where s.artistId = ?1 and s.title = ?2");
            query.setParameter(1, artistId);
            query.setParameter(2, title);
            Song song = null;
            List<Song> list = query.getResultList();
            if (list.size() > 0) {
                song = list.get(0);
            }
            return song;
        } finally {
            em.close();
        }
    }

}
